// GLOBAL FUNCTIONS UTILS

var JLREQUEST401 = "JLREQUEST:401";
var JLREQUEST408 = "JLREQUEST:408";

var JLHTTP_OK = 200;
var JLHTTP_BAD_REQUEST = 400;
var JLHTTP_NO_AUTHORIZED = 401;
var JLHTTP_REQUEST_TIMEOUT = 408;
var JLHTTP_ERROR = 500;

var JLMODE_ADD = 1;
var JLMODE_EDIT = 2;
var JLMODE_DELETE = 3;

var JLRESPONSE_ALERT = 1;
var JLRESPONSE_ERROR = 2;
var JLRESPONSE_INFO = 3;
var JLRESPONSE_OK = 4;

var JLMSG_DELAY = 1000 * 3;
	
$(function(){
    jQuery.fn.openPopup = function(width, height, close, onClose, escClose){
        if(width == null) width = 350;
        if(height == null) height = 350;
        if(close == null) close = false;
        if(onClose == null) onClose = null;
        if(escClose == null) escClose = false

        $(this).JLPopup({
            close: close,
            width: width,
            height: height,
            opacity: 50,
            escClose: escClose,
            onClose: onClose
        });
    };
    jQuery.closePopup = function(){
        $.JLPopup.close();
    };
    jQuery.fn.getIds = function() {
        var ids = $.clearIds($(this).jqGrid('getGridParam', 'selarrrow').toString());
        return $.trim(ids);
    };
    jQuery.fn.reloadGrid = function(){
        $(this).trigger("reloadGrid");
    };
    jQuery.fn.clearGridData = function(){
        $(this).jqGrid('clearGridData');
    };
    jQuery.fn.postDataGrid = function(data){
        $(this).jqGrid('setGridParam',{
            postData:data
        }).trigger("reloadGrid");
    };
    jQuery.fn.getId = function() {
        var ids = $(this).jqGrid('getGridParam', 'selrow');
        return (ids) ? $.trim(ids.toString()) : null;
    };
    jQuery.fn.getIdsArray = function() {
        var ids = $.trim($.clearIds($(this).jqGrid('getGridParam', 'selarrrow').toString()));
        if(ids) var a = ids.split(",");
        else a = [];
        return a;
    };
    jQuery.getRequestId = function(){
        return jQuery.JLREQUEST_ID;
    };
    $.extend({
        onErrorControls: function(xhr,st,err){
            /*var response = $.trim(xhr.responseText);
			if(JLREQUEST401 === response || JLREQUEST408 === response) {*/
            $.redirect(jQuery.JLURL_ROOT);
        //}
        },
        redirect: function(url){
            if(url==null) url=jQuery.JLURL_ROOT;
            document.location.replace(url);
        },
        request: function(url, data, callback, jsonData) {
            if(jQuery.isFunction(data)) {
                jsonData = callback;
                callback = data;
                data = {};
            }
			
            var xparams = {
                JLREQUEST_ID:jQuery.getRequestId(),
                xtime:new Date().getTime()
            };

            if(data != null){
                for(var obj in data)
                    xparams[obj] = data[obj];
            }
			
            if(!jsonData) jsonData = 'null';
            xparams["JLRequest"] = $.JSON.encode({
                JLRequest:jsonData
            });
			
            return jQuery.ajax({
                type: 'POST',
                url: jQuery.JLURL_ROOT + '/' + url,
                data: xparams,
                success: callback,
                dataType: 'json'
            });		
        },
        removeEndComa: function(e){
            return (e).replace(/\,+$/g,"");
        },
        clearIds: function(e){
            return e.replace(/\,+$|^\,/g,"");
        }
    });	
});